﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class PetTypeTable
    {
        private static PetTypeTable _instance;

        private static Logger _log = Logger.getLogger( typeof( PetTypeTable ).FullName );

        private Dictionary<int, L1PetType> _types = new Dictionary<int, L1PetType>();

        private HashSet<String> _defaultNames = new HashSet<String>();

        public static PetTypeTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new PetTypeTable();
            }
            return _instance;
        }

        private PetTypeTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM pettypes";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    int baseNpcId = SqlUtils.toInt( item["BaseNpcId"] );
                    String name = Convert.ToString( item["Name"] );
                    int itemIdForTaming = SqlUtils.toInt( item["ItemIdForTaming"] );
                    int hpUpMin = SqlUtils.toInt( item["HpUpMin"] );
                    int hpUpMax = SqlUtils.toInt( item["HpUpMax"] );
                    int mpUpMin = SqlUtils.toInt( item["MpUpMin"] );
                    int mpUpMax = SqlUtils.toInt( item["MpUpMax"] );
                    int npcIdForEvolving = SqlUtils.toInt( item["NpcIdForEvolving"] );
                    int[] msgIds = new int[5];
                    for ( int i = 0; i < 5; i++ )
                    {
                        msgIds[i] = SqlUtils.toInt( item["MessageId" + ( i + 1 )] );
                    }
                    int defyMsgId = SqlUtils.toInt( item["DefyMessageId"] );
                    IntRange hpUpRange = new IntRange( hpUpMin, hpUpMax );
                    IntRange mpUpRange = new IntRange( mpUpMin, mpUpMax );
                    _types[baseNpcId] = new L1PetType( baseNpcId, name,
                            itemIdForTaming, hpUpRange, mpUpRange,
                            npcIdForEvolving, msgIds, defyMsgId );
                    _defaultNames.Add( name.ToLower() );
                }
            }
        }

        public L1PetType get( int baseNpcId )
        {/*TTOK*/
            L1PetType ret;
            _types.TryGetValue( baseNpcId, out ret );
            return ret;
        }

        public bool isNameDefault( String name )
        {/*TTOK*/
            return _defaultNames.Contains( name.ToLower() );
        }
    }
}
